package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Strategy;
import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.domain.TravelCommend;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.domain.DestinationTemplate;
import cn.wolfcode.luowowo.search.domain.StrategyTemplate;
import cn.wolfcode.luowowo.search.domain.TravelTemplate;
import cn.wolfcode.luowowo.search.domain.UserInfoTemplate;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.vo.SearchResult;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class IndexController {

    @Reference
    private IUserInfoRedisService userInfoRedisService;
    @Reference
    private ITravelCommendService travelCommendService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IDestinationEsService destinationEsService;
    @Reference
    private IStrategyEsService strategyEsService;
    @Reference
    private ITravelEsService travelEsService;
    @Reference
    private IUserInfoEsService userInfoEsService;
    @Reference
    private ISearchService searchService;

    @RequestMapping("")
    public String index(Model model){

        //tcs
        List<TravelCommend> tcs = travelCommendService.getTop5();
        model.addAttribute("tcs", tcs);

        //sc
        StrategyCommend sc = strategyCommendService.getTop5().get(0);
        model.addAttribute("sc", sc);
        return "index/index";
    }

    @RequestMapping("/index/travelPage")
    public String travelPage(Model model, @ModelAttribute("qo")TravelQuery qo){
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo", pageInfo);

        return "/index/travelPageTpl";
    }

    @RequestMapping("/q")
    public String search(Model model, @ModelAttribute("qo") SearchQueryObject qo){
        switch (qo.getType()){
            case SearchQueryObject.SEARCH_TYPE_ALL:
                return this.searchAll(model, qo);
            case SearchQueryObject.SEARCH_TYPE_DEST:
                return this.searchDest(model, qo);
            case SearchQueryObject.SEARCH_TYPE_STRATEGY:
                return this.searchStrategy(model, qo);
            case SearchQueryObject.SEARCH_TYPE_TRAVEL:
                return this.searchTravel(model, qo);
            case SearchQueryObject.SEARCH_TYPE_USER:
                return this.searchUser(model, qo);
        }
        return "";
    }

    //用户
    private String searchUser(Model model, SearchQueryObject qo) {
        Page<UserInfoTemplate> users = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo,
                "nickname", "destName");
        model.addAttribute("page", users);
        return "/index/searchUser";
    }

    //游记
    private String searchTravel(Model model, SearchQueryObject qo) {
        Page<TravelTemplate> travels = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME, TravelTemplate.class, qo,
                "title", "summary");
        model.addAttribute("page", travels);
        return "/index/searchTravel";
    }

    //攻略
    private String searchStrategy(Model model, SearchQueryObject qo) {
        Page<StrategyTemplate> strategyTemplates = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo,
                "title", "subTitle", "summary");
        model.addAttribute("page", strategyTemplates);
        return "/index/searchStrategy";
    }

    //目的地
    private String searchDest(Model model, SearchQueryObject qo) {
        //根据传过来的keyword进行对应的搜索，搜索攻略，游记，用户
        //判断是否存在目的地
        DestinationTemplate dest = destinationEsService.findByName(qo.getKeyword());
        //如果存在的话就查询对应的数据
        if (dest != null) {
            List<StrategyTemplate> strategys = strategyEsService.findByDestName(dest.getName());
            List<TravelTemplate> travels = travelEsService.findByDestName(dest.getName());
            List<UserInfoTemplate> users = userInfoEsService.findByDestName(dest.getName());
            SearchResult result = new SearchResult();
            result.setStrategys(strategys);
            result.setTravels(travels);
            result.setUsers(users);
            //总数就等于全部查到的数据相加,小写long自己转类型
            long count = strategys.size() + travels.size() + users.size();
            result.setTotal(count);
            model.addAttribute("data", result);
        }
        model.addAttribute("dest",dest);
        return "/index/searchDest";
    }

    //全部
    private String searchAll(Model model, SearchQueryObject qo) {
        //目的地
        Page<DestinationTemplate> dests = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME, DestinationTemplate.TYPE_NAME, DestinationTemplate.class, qo,
                "name", "info");
        //攻略
        Page<StrategyTemplate> strategys = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME, StrategyTemplate.class, qo,
                "title", "subTitle", "summary");
        //游记
        Page<TravelTemplate> travels = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME, TravelTemplate.class, qo,
                "title", "summary");
        //用户
        Page<UserInfoTemplate> users = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME, UserInfoTemplate.class, qo,
                "nickname", "destName");
        SearchResult result = new SearchResult();
        result.setDests(dests.getContent());
        result.setStrategys(strategys.getContent());
        result.setTravels(travels.getContent());
        result.setUsers(users.getContent());
        result.setTotal(dests.getTotalElements() + strategys.getTotalElements() + travels.getTotalElements() + users.getTotalElements());
        model.addAttribute("data", result);
        return "/index/searchAll";
    }
}
